package cn.tlb.ui.custom.progressbutton;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.tlb.xutilsdownload.R;

/**
 * Created by 汤林冰 on 2015/12/3 15:51.
 */
public class ProgressButton extends RelativeLayout {
    private TextView textView;
    private ProgressBar progressBar;
    private static final String TEXT_DOWNLOAD = "下载";
    private static final String TEXT_DOWNLOADING = "取消";
    private static final String TEXT_INSTALL = "安装";
    private static final String COLOR_INSTALL = "#FF3CC4EF";

    private boolean downloading = false;
    public ProgressButton(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.progress_button, this);
        textView = (TextView) findViewById(R.id.textView);
        progressBar = (ProgressBar)findViewById(R.id.progress);
        progressBar.setProgress(100);
        progressBar.setMax(100);
    }

    public ProgressButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ProgressButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);

    }

    public void setProgress(int progress) {
        progressBar.setProgress(progress);
    }

    public void setSecondaryProgress(int progress) {
        progressBar.setSecondaryProgress(progress);
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        textView.setOnClickListener(l);
    }

    public void setText(CharSequence chars) {
        textView.setText(chars);
    }

    public void setText(int resId) {
        textView.setText(resId);
    }

    public int getProgress() {
        return progressBar.getProgress();
    }

    public int getMax() {
        return progressBar.getMax();
    }

    public boolean isDownloading() {
        return downloading;
    }

    public void setDownloading(boolean downloading) {
        this.downloading = downloading;
    }

    /**
     * prepare to download
     */
    public void defaultStyle(){
        downloading = false;
        progressBar.setProgress(100);
        textView.setText(TEXT_DOWNLOAD);
        textView.setBackgroundColor(Color.TRANSPARENT);
    }

    /**
     * downloading, means task can be canceled
     */
    public void downloadingStyle(){
        downloading = true;
        textView.setText(TEXT_DOWNLOADING);
        textView.setBackgroundColor(Color.TRANSPARENT);
    }

  /**
     * downloading, means task can be canceled
     */
    public void installStyle(){
        downloading = false;
        progressBar.setProgress(100);
        textView.setText(TEXT_INSTALL);
        textView.setBackgroundColor(Color.parseColor(COLOR_INSTALL));
    }


}
